package com.gaokao.demo.controller;


import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaokao.demo.entity.College;
import com.gaokao.demo.service.ICollegeService;
import com.gaokao.demo.vo.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author szs
 * @since 2021-01-22
 */
@RestController
@RequestMapping("/college")
public class CollegeController {

    @Autowired
    private ICollegeService collegeService;


    @RequestMapping(value = "/getCollegeList")
    @ResponseBody
    public String getCollegeList(@RequestBody String param) {
        HashMap hashMap = null;
        if (param == null) {
            return JSONUtil.toJsonStr(ResultResponse.newFail("未设置分页，请联系管理员"));
        } else {
            hashMap = JSONUtil.toBean(param, HashMap.class);
        }
        ResultResponse resultResponse = null;
        Integer page = Convert.toInt(hashMap.get("page"));
        Integer limit = Convert.toInt(hashMap.get("limit"));
        String collegeName = hashMap.get("collegeName") == null ? null : hashMap.get("collegeName").toString();
        List<String> classify = Convert.toList(String.class, hashMap.get("classify"));
        List<String> query = Convert.toList(String.class, hashMap.get("query"));
        IPage<College> iPage = new Page<>(page, limit);
        QueryWrapper<College> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag", 0);
        if (collegeName != null && collegeName != "") {
            queryWrapper.like("college_name", collegeName);
        }
        //分类 综合、农业、艺术
        if (classify != null && classify.size() > 0) {
            queryWrapper.in("classify", classify);
        }
        //985 211
        if (query != null && query.size() > 0) {
            for (String s : query) {
                if (s.equals("211")) {
                    queryWrapper.eq("is_211", 1);
                }
                if (s.equals("985")) {
                    queryWrapper.eq("is_985", 1);
                }
                if (s.equals("双一流")) {
                    queryWrapper.eq("is_double", 1);
                }
            }
        }
        IPage<College> collegeList = this.collegeService.getCollegeList(iPage, queryWrapper);
        HashMap<String, Object> map = new HashMap<>();
        if (collegeList != null && collegeList.getTotal() > 0) {
            map.put("total", collegeList.getTotal());
            map.put("items", collegeList.getRecords());
            resultResponse = ResultResponse.newSuccess("success", map);
        } else {
            map.put("total", 0);
            map.put("items", null);
            resultResponse = ResultResponse.newSuccess("success", map);
        }
        return JSONUtil.toJsonStr(resultResponse);
    }

    @RequestMapping(value = "/getClassifys")
    @ResponseBody
    public String getClassifys() {
        List<String> classifys = this.collegeService.getClassifys();
        ResultResponse resultResponse = null;
        if (classifys != null && classifys.size() > 0) {
            resultResponse = ResultResponse.newSuccess();
            HashMap<String, Object> map = new HashMap<>();
            map.put("classifys", classifys);
            resultResponse.setData(map);
            resultResponse.setMessage("success");
        } else {
            resultResponse = ResultResponse.newFail("获取学校分类失败！");
        }
        return JSONUtil.toJsonStr(resultResponse);
    }

    @RequestMapping(value = "/getSchoolBumens")
    @ResponseBody
    public String getSchoolBumens() {
        List schoolBumens = this.collegeService.getSchoolBumens();
        ResultResponse resultResponse = null;
        if (schoolBumens != null && schoolBumens.size() > 0) {
            resultResponse = ResultResponse.newSuccess();
            HashMap<String, Object> map = new HashMap<>();
            map.put("schoolBumens", schoolBumens);
            resultResponse.setData(map);
            resultResponse.setMessage("success");
        } else {
            resultResponse = ResultResponse.newFail("获取失败！");
        }

        return JSONUtil.toJsonStr(resultResponse);
    }


    @RequestMapping(value = "/deleteCollegeById")
    @ResponseBody
    public String deleteCollegeById(String id) {
        ResultResponse resultResponse = null;
        if (this.collegeService.deleteUserById(id)) {
            resultResponse = ResultResponse.newSuccess("删除成功");
            return JSONUtil.toJsonStr(resultResponse);
        } else {
            resultResponse = ResultResponse.newFail("删除失败");
            return JSONUtil.toJsonStr(resultResponse);
        }

    }

    @RequestMapping(value = "/createCollege")
    @ResponseBody
    public String createCollege(@RequestBody String map) {
        HashMap collegeMap = JSONUtil.toBean(map, HashMap.class);
        List schoolBumens = Convert.toList(collegeMap.get("schoolBumens"));
        List classify = Convert.toList(collegeMap.get("classify"));
        if (schoolBumens != null && schoolBumens.size() > 0) {
            collegeMap.put("schoolBumens", schoolBumens.get(0));
        }
        if (classify != null && classify.size() > 0) {
            collegeMap.put("classify", classify.get(0));
        }
        College college = null;
        college = Convert.convert(College.class, collegeMap);
        ResultResponse resultResponse = null;
        if (college != null) {
            this.collegeService.addCollege(college);
            resultResponse = ResultResponse.newSuccess("添加成功");
            HashMap<String, Object> data = new HashMap<>();
            data.put("id", college.getId());
            resultResponse.setData(data);
            return JSONUtil.toJsonStr(resultResponse);
        } else {
            resultResponse = ResultResponse.newSuccess("添加失败");
            return JSONUtil.toJsonStr(resultResponse);
        }
    }

}
